﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code.IDAL;
using Manager.App_Code.DAL;
using Manager.App_Code.DTO;
using Manager.FormMain;
using System.Linq;
using Manager.App_Code;
using Manager.Common;

namespace Manager
{
    public partial class frm_Country : DevExpress.XtraEditors.XtraForm
    {
        QuocGia qg = new QuocGia();
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();

        public frm_Country()
        {
            InitializeComponent();
            qg = null;
        }        

        public frm_Country(QuocGia _quocGia)
        {
            InitializeComponent();
            qg = _quocGia;
        }

        private void frm_Country_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
                this.butLuu_Click(sender, e);
            if (e.KeyCode == Keys.F4)
                this.butLuu_Thoat_Click(sender, e);
            if (e.KeyCode == Keys.Escape)
                this.butThoat_Click(sender, e);
           
        }

        private void frm_Country_Load(object sender, EventArgs e)
        {
            
            if (qg != null)
            {
                txtMa.Text = qg.MaQuocGia;
                txtTen.Text = qg.TenQuocGia;
                txtTenVT.Text = qg.TenVietTat;
                memoGhiChu.Text = qg.GhiChu;
                txtMa.Enabled = false;

            }

        }

        private int Validate()
        {
            int flag = 0;
            if (txtMa.Text == "")
            {
                flag = 1;
                dxErrorProvider1.SetError(txtMa, "Vui lòng nhập Mã Quốc Gia");
            }
            if (txtTen.Text == "")
            {
                flag = 1;
                dxErrorProvider1.SetError(txtTen, "Vui lòng nhập Tên Quốc Gia");
            }
            if (txtTenVT.Text == "")
            {
                flag = 1;
                dxErrorProvider1.SetError(txtTenVT, "Vui lòng nhập Tên viết tắt");
            }

            return flag;
        }

        private void butLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (qg != null)
                {
                    var temp = db.QuocGias.SingleOrDefault(i => i.MaQuocGia == qg.MaQuocGia);
                    temp.TenQuocGia = txtTen.Text;
                    temp.TenVietTat = txtTenVT.Text;
                    temp.MaQuocGia = txtMa.Text;
                    temp.GhiChu = memoGhiChu.Text;
                }
                else
                {
                    qg = new QuocGia();
                    qg.TenQuocGia = txtTen.Text;
                    qg.TenVietTat = txtTenVT.Text;
                    qg.MaQuocGia = txtMa.Text;
                    qg.GhiChu = memoGhiChu.Text;
                    db.QuocGias.InsertOnSubmit(qg);
                }                               
                db.SubmitChanges();
                MessageBox.Show("Lưu thành công.");
            }
            catch (Exception ex)
            {
                Log.WriteLog("frm_Country:butLuu_Click" + ex.Message + "===>" + ex.StackTrace);
                MessageBox.Show("Lỗi xảy ra!!!");
            }            
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có muốn đóng không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
                this.Close();
            
        }

        private void butLuu_Thoat_Click(object sender, EventArgs e)
        {
            this.butLuu_Click(sender, e);
            this.Close();
        }

    }
}